package com.dmai.capacity.platform.common.context;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.dmai.capacity.platform.common.model.BaseUser;
import com.dmai.capacity.platform.common.utils.OrikaUtils;

/**
 * 登录用户holder
 */
public class SysUserContextHolder {
	private static final ThreadLocal<BaseUser> CONTEXT = new TransmittableThreadLocal<BaseUser>() {
		@Override
		public BaseUser childValue(BaseUser parentValue) {
			return OrikaUtils.convert(parentValue, BaseUser.class);
		}

		@Override
		public BaseUser copy(BaseUser parentValue) {
			return OrikaUtils.convert(parentValue, BaseUser.class);
		}
	};

	public static void setUser(BaseUser user) {
		CONTEXT.set(user);
	}

	public static BaseUser getUser() {
		return CONTEXT.get();
	}

	public static void clear() {
		CONTEXT.remove();
	}
}